import {listRole, addRole, removeRole} from '_api/system/role'
import {_Promise} from "_libs/util";
import Vue from "vue";

export default {
  namespaced: true,
  state: {
    roleList: []
  },
  mutations: {
    setRoleList(state, roleList) {
      state.roleList = roleList
    },
    addRole(state, role) {
      state.roleList.unshift(role);
    },
    removeRole(state, role) {
      state.roleList = state.roleList.filter(existsRole => {
        if (existsRole.id !== role.id) {
          return existsRole;
        }
      })
    }
  },
  actions: {
    listRole({state, commit}) {
      return _Promise(listRole(), res => {
        commit('setRoleList', res.data)
      })
    },
    addRole({state, commit}, role) {
      return _Promise(addRole(role), res => {
        commit('addRole', res.data)
      })
    },
    removeRole({state, commit}, role) {
      return _Promise(removeRole(role.id), res => {
        commit('removeRole', role)
      })
    }
  }
}
